package com.fastCms.business.bim.manage.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fastCms.business.bim.enums.ReportTypeEnum;
import com.fastCms.business.bim.manage.WordManageService;
import com.fastCms.business.bim.pojo.entity.ReportData;
import com.fastCms.business.bim.pojo.excel.FootReportExcel;
import com.fastCms.business.bim.pojo.excel.PfReportExcel;
import com.fastCms.business.bim.pojo.vo.CarbonEmissionVO;
import com.fastCms.business.bim.pojo.vo.CarbonFootprintVO;
import com.fastCms.business.bim.pojo.vo.ImportReportListVO;
import com.fastCms.business.bim.service.ReportDataService;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.fastCms.business.frame.utils.BigDecimalUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Transactional(rollbackFor = Exception.class)
@Component
@Slf4j
public class WordManageServiceImpl implements WordManageService {

    @Resource
    private ReportDataService reportDataServiceImpl;

    public static final String SJ = "试件";

    public static final String SC = "生产";

    public static final String YH = "运输";

    public static final String HS = "回收";

    public static final String HSYH = "回收运输";

    public static final String GF = "光伏用量";

    public static final String low = "0.4kV电缆分支箱";
    public static final String high = "10kV电缆分支箱";
    public static final String jp = "JP柜";
    public static final String blq = "避雷器";
    public static final String ddx = "导、地线";
    public static final String dykg = "低压开关柜";
    public static final String dlbh = "电缆保护管";
    public static final String dldl = "电力电缆";
    public static final String dlq = "断路器(柱上开关设备)";
    public static final String gykg = "高压开关柜";
    public static final String gl = "隔离开关";
    public static final String hw = "环网柜";
    public static final String jk = "架空绝缘线";
    public static final String jj = "金具";
    public static final String os = "欧式箱式变电站";
    public static final String jyz = "盘形瓷、玻璃绝缘子";
    public static final String byq = "配电变压器";
    public static final String tc = "塔材";
    public static final String hfjyz = "支柱式瓷、复合绝缘子";

    public static final String y1 = "1月光伏发电量";
    public static final String y2 = "2月光伏发电量";
    public static final String y3 = "3月光伏发电量";
    public static final String y4 = "4月光伏发电量";
    public static final String y5 = "5月光伏发电量";
    public static final String y6 = "6月光伏发电量";
    public static final String y7 = "7月光伏发电量";
    public static final String y8 = "8月光伏发电量";
    public static final String y9 = "9月光伏发电量";
    public static final String y10 = "10月光伏发电量";
    public static final String y11 = "11月光伏发电量";
    public static final String y12 = "12月光伏发电量";


    @Override
    public CarbonFootprintVO exportCarbonFootprint(String serialNo) {
        ReportData reportData = reportDataServiceImpl.findBySerialNo(serialNo);

        String json = reportData.getReportData();
        List<FootReportExcel> reportExcel = JSON.parseArray(json, FootReportExcel.class);
        List<FootReportExcel> yclExcelList = reportExcel.stream().filter(item -> item.getType().equals("原材料")).collect(Collectors.toList());
        List<FootReportExcel> dExcelList = reportExcel.stream().filter(item -> item.getType().equals("产品生产") && item.getName().equals("电"))
            .collect(Collectors.toList());
        FootReportExcel dExcel = dExcelList.get(0);

        List<FootReportExcel> cyExcelList = reportExcel.stream().filter(item -> item.getType().equals("产品生产") && item.getName().equals("汽油"))
            .collect(Collectors.toList());
        FootReportExcel cyExcel = cyExcelList.get(0);
        Date now = new Date();

        CarbonFootprintVO carbonFootprintVO = new CarbonFootprintVO();
        //企业基本信息
        ReportData company = reportDataServiceImpl.findCompany();
        String data = company.getReportData();
        if (StringUtils.isNotBlank(data)) {
            CarbonFootprintVO.Company existCompany = JSON.parseObject(data, CarbonFootprintVO.Company.class);
            carbonFootprintVO.setCompany(existCompany);
        }
        //时间相关
        carbonFootprintVO.setToday(DateUtil.format(now, "yyyy-MM-dd"));
        carbonFootprintVO.setStartDay(DateUtil.format(now, "yyyy-MM-dd"));
        carbonFootprintVO.setEndDay(DateUtil.format(DateUtils.addDays(now, 7), "yyyy-MM-dd"));

        BigDecimal sczpf = new BigDecimal(0);
        BigDecimal yszpf = new BigDecimal(0);
        BigDecimal nyzpf = new BigDecimal(0);
        BigDecimal zpf = new BigDecimal(0);

        String scpfzzb;
        String yspfzzb;
        String dpfzb;
        String cypfzb;
        String nypfzzb;

        final String startMerge = "<w:vmerge w:val='restart'/>";
        final String endMerge = "<w:vmerge/>";
        //原材料相关
        List<CarbonFootprintVO.Ycl> yclList = Lists.newArrayList();
        for (FootReportExcel yclExcel : yclExcelList) {
            CarbonFootprintVO.Ycl ycl = new CarbonFootprintVO.Ycl();
            ycl.setName(yclExcel.getName());
            ycl.setNum(yclExcel.getNum());
            ycl.setPfyz(yclExcel.getPfyz());
            ycl.setYsfs(yclExcel.getYsfs());
            ycl.setYsjl(yclExcel.getYsjl());
            ycl.setYsyz(yclExcel.getYsyz());
            ycl.setEndMerge(endMerge);

            //生产排放
            BigDecimal scpf = BigDecimalUtil.multiply(yclExcel.getNum(), yclExcel.getPfyz());
            ycl.setScpf(scpf);
            sczpf = BigDecimalUtil.add(sczpf, scpf);//生成总排放

            //运输排放
            BigDecimal yspf = BigDecimalUtil.multiply(BigDecimalUtil.multiply(BigDecimalUtil.multiply(yclExcel.getNum(), yclExcel.getYsjl()), yclExcel.getYsyz()), new BigDecimal(1));
            ycl.setYspf(yspf);
            yszpf = BigDecimalUtil.add(yszpf, yspf);//生成总排放

            yclList.add(ycl);
        }
        yclList.get(0).setStartMerge(startMerge);
        yclList.get(0).setEndMerge(null);


        carbonFootprintVO.setYclList(yclList);

        carbonFootprintVO.setSczpf(sczpf);
        carbonFootprintVO.setYszpf(yszpf);

        //电计算
        carbonFootprintVO.setDyl(dExcel.getNum());
        BigDecimal dpf = BigDecimalUtil.multiply(dExcel.getNum(), new BigDecimal(0.5703));
        carbonFootprintVO.setDpf(dpf);

        //油计算
        //油因子
        Double yyz = 44.80 * 0.0189 * 0.98 * 44;
        carbonFootprintVO.setCyyl(cyExcel.getNum());
        BigDecimal cypf = BigDecimalUtil.divide(BigDecimalUtil.multiply(cyExcel.getNum(), new BigDecimal(yyz)), new BigDecimal(12));
        carbonFootprintVO.setCypf(cypf);

        nyzpf = BigDecimalUtil.add(dpf, cypf);

        //总排放
        carbonFootprintVO.setNyzpf(nyzpf);
        zpf = BigDecimalUtil.add(BigDecimalUtil.add(sczpf, yszpf), nyzpf);


        for (CarbonFootprintVO.Ycl ycl : carbonFootprintVO.getYclList()) {
            ycl.setScpfzb(BigDecimalUtil.multiply(BigDecimalUtil.divide(ycl.getScpf(), zpf), new BigDecimal(100)) + "%");
            ycl.setYspfzb(BigDecimalUtil.multiply(BigDecimalUtil.divide(ycl.getYspf(), zpf), new BigDecimal(100)) + "%");
        }

        //占比计算
        scpfzzb = BigDecimalUtil.multiply(BigDecimalUtil.divide(sczpf, zpf), new BigDecimal(100)) + "%";
        yspfzzb = BigDecimalUtil.multiply(BigDecimalUtil.divide(yszpf, zpf), new BigDecimal(100)) + "%";
        cypfzb = BigDecimalUtil.multiply(BigDecimalUtil.divide(cypf, zpf), new BigDecimal(100)) + "%";
        dpfzb = BigDecimalUtil.multiply(BigDecimalUtil.divide(dpf, zpf), new BigDecimal(100)) + "%";
        nypfzzb = BigDecimalUtil.multiply(BigDecimalUtil.divide(nyzpf, zpf), new BigDecimal(100)) + "%";


        carbonFootprintVO.setScpfzzb(scpfzzb);
        carbonFootprintVO.setYspfzzb(yspfzzb);
        carbonFootprintVO.setDpfzb(dpfzb);
        carbonFootprintVO.setCypfzb(cypfzb);


        carbonFootprintVO.setNypfzzb(nypfzzb);
        carbonFootprintVO.setZpf(zpf);

        return carbonFootprintVO;
    }

    @Override
    public CarbonEmissionVO exportCarbonEmission(String serialNo) {

        ReportData reportData = reportDataServiceImpl.findBySerialNo(serialNo);
        if (reportData == null) {
            return null;
        }

        List<PfReportExcel> pfReportExcels = JSON.parseObject(reportData.getReportData(), new TypeReference<List<PfReportExcel>>() {
        });

        CarbonEmissionVO carbonEmissionVO = new CarbonEmissionVO();
        pfReportExcels.forEach(pfReportExcel -> {
            buildCarbonEmissionVO(pfReportExcel, carbonEmissionVO);
        });
        process(carbonEmissionVO);
        Date now = new Date();
        CarbonEmissionVO.CarBonTime carBonTime = new CarbonEmissionVO.CarBonTime();
        carBonTime.setToday(DateUtil.format(now, "yyyy-MM-dd"));
        carBonTime.setStartDay(DateUtil.format(now, "yyyy-MM-dd"));
        carBonTime.setEndDay(DateUtil.format(DateUtils.addDays(now, 7), "yyyy-MM-dd"));
        carbonEmissionVO.setCarBonTime(carBonTime);

        return carbonEmissionVO;
    }

    private void process(CarbonEmissionVO carbonEmissionVO) {

        carbonEmissionVO.getDyl().setAll(jisuanAdd(carbonEmissionVO.getDyl().getBgq(), carbonEmissionVO.getDyl().getJcq()));
        carbonEmissionVO.getDtpf().setAll(jisuanAdd(carbonEmissionVO.getDtpf().getBgq(), carbonEmissionVO.getDtpf().getJcq()));
        carbonEmissionVO.getSyl().setAll(jisuanAdd(carbonEmissionVO.getSyl().getBgq(), carbonEmissionVO.getSyl().getJcq()));
        carbonEmissionVO.getStpf().setAll(jisuanAdd(carbonEmissionVO.getStpf().getBgq(), carbonEmissionVO.getStpf().getJcq()));


        // todo fix
        carbonEmissionVO.getDtpf().setAll(jisuanAdd(carbonEmissionVO.getDtpf().getBlq(),
            carbonEmissionVO.getDtpf().getDdx(),
            carbonEmissionVO.getDtpf().getDlbhg(),
            carbonEmissionVO.getDtpf().getDldl(),
            carbonEmissionVO.getDtpf().getJkjyx(),
            carbonEmissionVO.getDtpf().getJj(),
            carbonEmissionVO.getDtpf().getBljyz(),
            carbonEmissionVO.getDtpf().getTc(),
            carbonEmissionVO.getDtpf().getHhjyz()));


        carbonEmissionVO.getTpfl().setAll(jisuanAdd(
            carbonEmissionVO.getTpfl().getLow(),
            carbonEmissionVO.getTpfl().getHigh(),
            carbonEmissionVO.getTpfl().getJp(),
            carbonEmissionVO.getTpfl().getBlq(),
            carbonEmissionVO.getTpfl().getDdx(),
            carbonEmissionVO.getTpfl().getDykg(),
            carbonEmissionVO.getTpfl().getDlbhg(),
            carbonEmissionVO.getTpfl().getDldl(),
            carbonEmissionVO.getTpfl().getDlq(),
            carbonEmissionVO.getTpfl().getGykg(),
            carbonEmissionVO.getTpfl().getGlkg(),
            carbonEmissionVO.getTpfl().getHwg(),
            carbonEmissionVO.getTpfl().getJkjyx(),
            carbonEmissionVO.getTpfl().getJj(),
            carbonEmissionVO.getTpfl().getOsbdz(),
            carbonEmissionVO.getTpfl().getBljjz(),
            carbonEmissionVO.getTpfl().getPdbyq(),
            carbonEmissionVO.getTpfl().getTc(),
            carbonEmissionVO.getTpfl().getFhjyz()));

        carbonEmissionVO.getHstpfl().setAll(jisuanAdd(carbonEmissionVO.getDtpf().getBlq(),
            carbonEmissionVO.getHstpfl().getDdx(),
            carbonEmissionVO.getHstpfl().getDlbh(),
            carbonEmissionVO.getHstpfl().getDldl(),
            carbonEmissionVO.getHstpfl().getJkjyx(),
            carbonEmissionVO.getHstpfl().getJj(),
            carbonEmissionVO.getHstpfl().getBljyz(),
            carbonEmissionVO.getHstpfl().getTc()));

        carbonEmissionVO.getHsystpfl().setAll(jisuanAdd(carbonEmissionVO.getDtpf().getBlq(),
            carbonEmissionVO.getHsystpfl().getDdx(),
            carbonEmissionVO.getHsystpfl().getDlbh(),
            carbonEmissionVO.getHsystpfl().getDldl(),
            carbonEmissionVO.getHsystpfl().getJkjyx(),
            carbonEmissionVO.getHsystpfl().getJj(),
            carbonEmissionVO.getHsystpfl().getBljyz(),
            carbonEmissionVO.getHsystpfl().getTc()));

        BigDecimal hsAlltp = new BigDecimal(carbonEmissionVO.getHsystpfl().getAll()).subtract(new BigDecimal(carbonEmissionVO.getHstpfl().getAll()));
        carbonEmissionVO.setHsAllTp(hsAlltp.toString());

        carbonEmissionVO.setAllTp(jisuanAdd(carbonEmissionVO.getDtpf().getAll(), carbonEmissionVO.getTpfl().getAll(), hsAlltp.toString()));
        carbonEmissionVO.getNtpf().setAll(jisuanAdd(
            carbonEmissionVO.getNtpf().getNov(),
            carbonEmissionVO.getNtpf().getFeb(),
            carbonEmissionVO.getNtpf().getMar(),
            carbonEmissionVO.getNtpf().getApr(),
            carbonEmissionVO.getNtpf().getMay(),
            carbonEmissionVO.getNtpf().getJun(),
            carbonEmissionVO.getNtpf().getJul(),
            carbonEmissionVO.getNtpf().getAug(),
            carbonEmissionVO.getNtpf().getJan(),
            carbonEmissionVO.getNtpf().getSep(),
            carbonEmissionVO.getNtpf().getOct(),
            carbonEmissionVO.getNtpf().getDec()
        ));


    }


    /**
     * 计算碳排放
     *
     * @param tpfyz
     * @param target
     * @return
     */
    public String jisuanAdd(String tpfyz, String target) {
        BigDecimal t = new BigDecimal(tpfyz);
        BigDecimal tpf = t.add(new BigDecimal(target)).setScale(4, BigDecimal.ROUND_DOWN);
        return tpf.toString();
    }

    public String jisuanAdd(String... params) {

        BigDecimal res = new BigDecimal(0);
        for (String param : params) {
            BigDecimal t = new BigDecimal(param);
            res = res.add(t);
        }
        return res.toString();
    }


    /**
     * 计算碳排放
     *
     * @param tpfyz
     * @param target
     * @return
     */
    public String jisuan(String tpfyz, String target) {
        BigDecimal t = new BigDecimal(tpfyz);
        BigDecimal tpf = t.multiply(new BigDecimal(target)).setScale(4, BigDecimal.ROUND_DOWN);
        return tpf.toString();
    }


    /**
     * 计算碳排放
     *
     * @param target
     * @return
     */
    public String jisuan(BigDecimal t1, String target) {
        BigDecimal tpf = t1.multiply(new BigDecimal(target)).setScale(4, BigDecimal.ROUND_DOWN);
        return tpf.toString();
    }

    /**
     * 计算碳排放
     *
     * @param tpfyz
     * @param target
     * @return
     */
    public String jisuan(String tpfyz, String zl, String target) {
        BigDecimal t = new BigDecimal(tpfyz);
        BigDecimal t2 = new BigDecimal(zl);
        BigDecimal t3 = new BigDecimal(target);
        BigDecimal tpf = t.multiply(t2).setScale(4, BigDecimal.ROUND_DOWN);
        tpf = tpf.multiply(t3).setScale(4, BigDecimal.ROUND_UP);
        return tpf.toString();
    }

    public String zzl(PfReportExcel pfReportExcel) {
        return jisuan(pfReportExcel.getNum(), pfReportExcel.getZl());
    }

    private void buildCarbonEmissionVO(PfReportExcel pfReportExcel, CarbonEmissionVO carbonEmissionVO) {

        if (SJ.equals(pfReportExcel.getType())) {
            if ("检测车汽油".equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDyl().setJccqy(pfReportExcel.getNum());
                BigDecimal t = new BigDecimal(304.25472);
                BigDecimal tpf = t.multiply(new BigDecimal(pfReportExcel.getNum())).setScale(2, BigDecimal.ROUND_DOWN);
                carbonEmissionVO.getStpf().setJccqy(tpf.toString());
            }
            if ("办公区电用量".equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setBgq(jisuan(pfReportExcel.getPfyz(), pfReportExcel.getNum()));
                carbonEmissionVO.getDyl().setBgq(pfReportExcel.getNum());
            }
            if ("检测区电用量".equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setJcq(jisuan(pfReportExcel.getPfyz(), pfReportExcel.getNum()));
                carbonEmissionVO.getDyl().setJcq(pfReportExcel.getNum());
            }
            if ("办公区水用量".equals(pfReportExcel.getName())) {
                carbonEmissionVO.getStpf().setBgq(jisuan(pfReportExcel.getPfyz(), pfReportExcel.getNum()));
                carbonEmissionVO.getSyl().setBgq(pfReportExcel.getNum());
            }
            if ("检测区水用量".equals(pfReportExcel.getName())) {
                carbonEmissionVO.getStpf().setJcq(jisuan(pfReportExcel.getPfyz(), pfReportExcel.getNum()));
                carbonEmissionVO.getSyl().setJcq(pfReportExcel.getNum());
            }
            if ("A4纸用量".equals(pfReportExcel.getName())) {
                carbonEmissionVO.getSczl().setA4(pfReportExcel.getZl());
                carbonEmissionVO.getDyl().setA4(pfReportExcel.getNum());
                carbonEmissionVO.getDtpf().setJcq(jisuan(pfReportExcel.getPfyz(), pfReportExcel.getZl(), pfReportExcel.getNum()));
            }
        }
        if (SC.equals(pfReportExcel.getType())) {
            if (blq.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getDtpf().setBlq(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setBlq(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setBlq(pfReportExcel.getNum());
            }
            if (ddx.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getDtpf().setDdx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setDdx(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setDdx(pfReportExcel.getNum());
            }
            if (dlbh.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setDlbhg(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setDlbhg(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setDlbhg(pfReportExcel.getNum());
            }
            if (dldl.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setDldl(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setDldl(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setDldl(pfReportExcel.getNum());
            }
            if (jk.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setJkjyx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setJkjyx(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setJkjyx(pfReportExcel.getNum());
            }
            if (jj.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setJj(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setJj(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setJj(pfReportExcel.getNum());
            }
            if (jyz.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setBljyz(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setBljyz(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setBljyz(pfReportExcel.getNum());
            }
            if (tc.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setTc(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setTc(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setTc(pfReportExcel.getNum());
            }
            if (hfjyz.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getDtpf().setHhjyz(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getSczl().setHhjyz(zzl(pfReportExcel));
                carbonEmissionVO.getDyl().setHhjyz(pfReportExcel.getNum());
            }

        }
        if (YH.equals(pfReportExcel.getType())) {
            if (low.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getTpfl().setLow(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setLow(zzl(pfReportExcel));

                carbonEmissionVO.getSl().setLow(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setLow(pfReportExcel.getYj());
            }
            if (high.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getTpfl().setHigh(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setHigh(zzl(pfReportExcel));

                carbonEmissionVO.getSl().setHigh(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setHigh(pfReportExcel.getYj());
            }
            if (jp.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setJp(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setJp(zzl(pfReportExcel));

                carbonEmissionVO.getSl().setJp(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setJp(pfReportExcel.getYj());
            }
            if (blq.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setBlq(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setBlq(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setBlq(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setBlq(pfReportExcel.getYj());
            }
            if (ddx.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDdx(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDdx(zzl(pfReportExcel));

                carbonEmissionVO.getSl().setDdx(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setDdx(pfReportExcel.getYj());
            }
            if (dykg.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDykg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDykg(zzl(pfReportExcel));

                carbonEmissionVO.getSl().setDykg(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setDykg(pfReportExcel.getYj());
            }
            if (dlbh.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getTpfl().setDlbhg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDlbhg(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setDlbhg(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setDlbhg(pfReportExcel.getYj());
            }


            if (dldl.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDldl(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDldl(zzl(pfReportExcel));

                carbonEmissionVO.getSl().setDldl(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setDldl(pfReportExcel.getYj());
            }
            if (dlq.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDlq(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDlq(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setDlq(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setDlq(pfReportExcel.getYj());
            }
            if (gykg.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setGykg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setGykg(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setGykg(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setGykg(pfReportExcel.getYj());
            }

            if (gl.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDlbhg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDlbhg(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setGlkg(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setGlkg(pfReportExcel.getYj());
            }

            if (hw.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setHwg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setHwg(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setHwg(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setHwg(pfReportExcel.getYj());
            }
            if (jk.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setJkjyx(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setJkjyx(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setJkjyx(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setJkjyx(pfReportExcel.getYj());
            }
            if (jj.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDlbhg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDlbhg(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setJj(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setJj(pfReportExcel.getYj());
            }

            if (os.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setOsbdz(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setOsbdz(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setOsbdz(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setOsbdz(pfReportExcel.getYj());
            }
            if (jyz.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setDlbhg(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setDlbhg(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setBljjz(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setBljjz(pfReportExcel.getYj());
            }

            if (byq.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setPdbyq(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setPdbyq(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setPdbyq(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setPdbyq(pfReportExcel.getYj());
            }
            if (tc.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setTc(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setTc(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setTc(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setTc(pfReportExcel.getYj());
            }

            if (hfjyz.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getTpfl().setFhjyz(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getZzl().setFhjyz(zzl(pfReportExcel));
                carbonEmissionVO.getSl().setFhjyz(pfReportExcel.getNum());
                carbonEmissionVO.getYj().setFhjyz(pfReportExcel.getYj());
            }
        }


        if (HS.equals(pfReportExcel.getType())) {
            if (blq.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getHstpfl().setBlq(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setBlq(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setBlq(pfReportExcel.getNum());
            }
            if (ddx.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHstpfl().setDdx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setDdx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setDdx(pfReportExcel.getNum());
            }
            if (dlbh.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getHstpfl().setDlbh(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setDlbh(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setDlbh(pfReportExcel.getNum());
            }
            if (dldl.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHstpfl().setDldl(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setDldl(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setDldl(pfReportExcel.getNum());
            }
            if (jk.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHstpfl().setJkjyx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setJkjyx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setJkjyx(pfReportExcel.getNum());
            }
            if (jj.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHstpfl().setJj(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setJj(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setJj(pfReportExcel.getNum());
            }
            if (jyz.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHstpfl().setBljyz(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setBljyz(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setBljyz(pfReportExcel.getNum());
            }
            if (tc.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHstpfl().setTc(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHszzl().setTc(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHssl().setTc(pfReportExcel.getNum());
            }
        }

        if (HSYH.equals(pfReportExcel.getType())) {
            if (blq.equals(pfReportExcel.getName())) {

                carbonEmissionVO.getHsystpfl().setBlq(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setBlq(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setBlq(pfReportExcel.getNum());
            }
            if (ddx.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setDdx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setDdx(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setDdx(pfReportExcel.getNum());
            }
            if (dlbh.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setDlbh(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setDlbh(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setDlbh(pfReportExcel.getNum());
            }
            if (dldl.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setDldl(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setDldl(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setDldl(pfReportExcel.getNum());
            }
            if (jk.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setJkjyx(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setJkjyx(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setJkjyx(pfReportExcel.getNum());
            }
            if (jj.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setJj(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setJj(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setJj(pfReportExcel.getNum());
            }
            if (jyz.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setBljyz(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setBljyz(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setBljyz(pfReportExcel.getNum());
            }
            if (tc.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getHsystpfl().setTc(jisuan(zzl(pfReportExcel), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyszl().setTc(jisuan(zzl(pfReportExcel), pfReportExcel.getYj(), pfReportExcel.getPfyz()));
                carbonEmissionVO.getHsyssl().setTc(pfReportExcel.getNum());
            }
        }
        if (GF.equals(pfReportExcel.getType())) {
            if (y1.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setJan(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setJan(pfReportExcel.getNum());
            }
            if (y2.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setFeb(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setFeb(pfReportExcel.getNum());
            }
            if (y3.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setMar(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setMar(pfReportExcel.getNum());
            }
            if (y4.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setApr(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setApr(pfReportExcel.getNum());
            }
            if (y5.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setMay(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setMay(pfReportExcel.getNum());
            }
            if (y6.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setJun(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setJun(pfReportExcel.getNum());
            }
            if (y7.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setJul(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setJul(pfReportExcel.getNum());
            }
            if (y8.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setAug(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setAug(pfReportExcel.getNum());
            }
            if (y9.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setSep(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setSep(pfReportExcel.getNum());
            }
            if (y10.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setOct(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setOct(pfReportExcel.getNum());
            }
            if (y11.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setNov(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setNov(pfReportExcel.getNum());
            }
            if (y12.equals(pfReportExcel.getName())) {
                carbonEmissionVO.getNtpf().setDec(jisuan(new BigDecimal("0.5703"), pfReportExcel.getNum()));
                carbonEmissionVO.getFdl().setDec(pfReportExcel.getNum());
            }

        }

    }

    @Override
    public CarbonFootprintVO.Company getCompany() {
        ReportData company = reportDataServiceImpl.findCompany();
        if (null != company && StringUtils.isNotBlank(company.getReportData())) {
            CarbonFootprintVO.Company result = JSON.parseObject(company.getReportData(), CarbonFootprintVO.Company.class);
            return result;
        }
        return null;
    }

    @Override
    public Boolean saveCompany(CarbonFootprintVO.Company company) {
        ReportData existCompany = reportDataServiceImpl.findCompany();
        Date now = new Date();
        if (null == existCompany) {
            existCompany = new ReportData();
            existCompany.setReportType(ReportTypeEnum.COMPANY.getType());
        }
        existCompany.setReportData(JSON.toJSONString(company));
        existCompany.setImportTime(now);
        existCompany.setSerialNo(now.getTime() + "");
        existCompany.setCreateTime(now);
        reportDataServiceImpl.saveOrUpdate(existCompany);
        return true;
    }

    @Override
    public List<ImportReportListVO> importReportList(String type) {
        List<ReportData> result = reportDataServiceImpl.findValidAll(type, 10);
        return BeanConvertUtil.convertList(result, ImportReportListVO.class);
    }

    @Override
    public Boolean delete(String reportSerialNo) {
        reportDataServiceImpl.deleteBySerialNo(reportSerialNo);
        return true;
    }

    @Override
    public Boolean importPf(List<PfReportExcel> excels) {
        Date now = new Date();
        ReportData reportData = new ReportData();
        reportData.setReportData(JSON.toJSONString(excels));
        reportData.setReportType(ReportTypeEnum.TPF.getType());
        reportData.setImportTime(now);
        reportData.setCreateTime(now);
        reportData.setUpdateTime(now);
        reportData.setSerialNo(now.getTime() + "");
        reportData.setState(1);
        reportDataServiceImpl.save(reportData);
        return true;
    }

    @Override
    public Boolean importFoot(List<FootReportExcel> excels) {
        Date now = new Date();
        ReportData reportData = new ReportData();
        reportData.setReportData(JSON.toJSONString(excels));
        reportData.setReportType(ReportTypeEnum.TZJ.getType());
        reportData.setImportTime(now);
        reportData.setCreateTime(now);
        reportData.setUpdateTime(now);
        reportData.setSerialNo(now.getTime() + "");
        reportData.setState(1);
        reportDataServiceImpl.save(reportData);
        return true;
    }

}
